/**
 * @author Syang4
 */

var QS_watchlistDataExporter=null;
var watchlistDataExporter = function(){
	this.data = [];
	this.cols = [];
	this.headers=[];
	this.el='';
	
	this.cfg = {};
};

watchlistDataExporter.prototype = {
	reBindCfg: function(cfg){
		$.extend(this.cfg, cfg);
	},
	reset: function(){
		this.data = [];
		this.cols = [];
		this.headers = [];
		this.wlObj = null;
		this.blacklist = " M* Rating ___ Up/Down ___ Alert ";
	},
	exportData: function(cols, data, cfg){
		this.reset();
		this.data = data;
		this.getCols(cols);
		this.reBindCfg(cfg)
		this.sendRequest();
	},
	getCols: function(cols){
		for (var c in cols) {
			if (cols[c].val != 'undefined' && typeof(cols[c].renderer) == 'undefined' && (typeof(cols[c].hide) == 'undefined' || cols[c].hide != true)) {
				var header = cols[c].txt.replace(/<[^>]*>/ig, '');
				if (this.blacklist.indexOf(header) != -1) {
					continue;
				}
				
				if (cols[c].val.indexOf("%") != -1) {
					this.cols.push(cols[c].val.replace(/%/g, '^__^'));
				}
				else {
					this.cols.push(cols[c].val);
				}
				
				if (cols[c].txt.indexOf("%") != -1) {
					this.headers.push(header.replace(/%/g, '^__^'));
				}
				else {
					this.headers.push(header);
				}
			}
		}
	},
	
	/**
	packData: function(){
		var currentId = this.wlObj.profile.currentId;
		var self = this;
		QS_watchlistDataManager.getHoldings(currentId, function(holdings){
			self._packData(holdings);
		});
	},
	_packData: function(holdings){
		var domMap = this.wlObj.grid.DOMMap;
		for (var i = 0, len = holdings.length; i < len; i++) {
			var d = {}, holdingId = holdings[i]["HldgId"];
			for (var idx = 0; idx < this.cols.length; idx++) {
				var field = this.cols[idx].indexOf("^__^") != -1 ? this.cols[idx].replace(/\^__\^/g, '%') : this.cols[idx];
				if (field == 'Up/Down') {
					d[this.cols[idx]] = ""
				}
				else {
					if (typeof(domMap[holdingId]) == 'undefined' || typeof(domMap[holdingId][field]) == 'undefined' || !domMap[holdingId][field] || domMap[holdingId][field] == null) {
						d[this.cols[idx]] = "";
					}
					else {
						var dom = domMap[holdingId][field];
						var txt = dom.innerText ? dom.innerText : dom.textContent;
						d[this.cols[idx]] = (!!txt ? txt : "");
					}
					
				}
			}
			this.data.push(d);
		}
	},*/
	sendRequest: function(){
		var name = this.cfg.name;
		var currentID = this.cfg.currentID;
		var currentType = this.cfg.currentType;
		var currentSrc = this.cfg.currentSrc;
		var type = "WatchList";
		if (currentSrc == "default" || currentSrc == "QS") {
			type = (currentType == 1 ? "WatchList" : "Portfolio");
		}
		else 
			if (currentSrc == "LPLH" || currentSrc == "LPLP") {
				type = (currentSrc == "LPLH" ? "Household" : "Practice");
			}
		
		var keys = ["type", "name", "headers", "cols", "data"];
		var values = [type, name, "[" + this.headers.join(",") + "]", "[" + this.cols.join(",") + "]", $.toJSON(this.data)];
		var url = "exportWatchlistToExcel.jsp";
		this.openWindowWithPost(url, "exportWatchListDataHidFrm", keys, values);
	},
	openWindowWithPost: function(url, name, keys, values){
		if (typeof(window.frames[name]) == 'undefined') {
			var html = "<div class='exportWatchListData' style='display:none;'>";
			html += "<form id='exportWatchListDataform' name='exportWatchListDataform' method='post' action='" + url + "' target='" + name + "'>";
			html += "<iframe id='" + name + "' name='" + name + "' style='display:none;'></iframe>";
			html += "</form>";
			html += "</div>";
			this.el = $(html);
			this.el.appendTo($('body'));
		}
		
		this.el.find("form").each(function(){
			this.reset();
		});
		if (keys && values && (keys.length == values.length)) 
			for (var i = 0; i < keys.length; i++) {
				if (this.el.find("form > input[name=" + keys[i] + "]").size() <= 0) {
					this.el.find("form").append("<input type='hidden' name='" + keys[i] + "' value=''/>");
				}
				this.el.find("form > input[name=" + keys[i] + "]").first().val(values[i]);
			}
		this.el.find("form").each(function(){
			this.submit();
		});
	}
}